#include "test/test.h"

#define DISP_BUF_SIZE (480 * 1024)

#if 0

/*********************************btn***********************************/
// 回调函数
static void event_handler(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);

    if(code == LV_EVENT_CLICKED) {
        LV_LOG_USER("Clicked");
    } else if(code == LV_EVENT_VALUE_CHANGED) {
        LV_LOG_USER("Toggled");
    }
}

void lv_bling_btn_1(void)
{
    lv_obj_t * label;

    lv_obj_t * btn1 = lv_btn_create(lv_scr_act());
    lv_obj_add_event_cb(btn1, event_handler, LV_EVENT_ALL, NULL);
    lv_obj_align(btn1, LV_ALIGN_CENTER, 0, -40);

    label = lv_label_create(btn1);
    lv_label_set_text(label, "Button");
    lv_obj_center(label);

    lv_obj_t * btn2 = lv_btn_create(lv_scr_act());
    lv_obj_add_event_cb(btn2, event_handler, LV_EVENT_ALL, NULL);
    lv_obj_align(btn2, LV_ALIGN_CENTER, 0, 40);
    lv_obj_add_flag(btn2, LV_OBJ_FLAG_CHECKABLE);
    lv_obj_set_height(btn2, LV_SIZE_CONTENT);

    label = lv_label_create(btn2);
    lv_label_set_text(label, "Toggle");
    lv_obj_center(label);
}

/*
    4个按键的用法
*/
// 按键1回调函数
static void event_handler1(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    char * user_data     = (char *)lv_event_get_user_data(e); // 传的字符串

    if(code == LV_EVENT_CLICKED)
        // LV_LOG_USER("Clicked");
        printf("anjian1:%s\n", user_data);
}
// 按键2回调函数
static void event_handler2(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    if(code == LV_EVENT_CLICKED)
        // LV_LOG_USER("Clicked");
        printf("anjian2\n");
}
// 按键3回调函数
static void event_handler3(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);

    if(code == LV_EVENT_CLICKED)
        // LV_LOG_USER("Clicked");
        printf("anjian3\n");
}
// 按键4回调函数
static void event_handler4(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);

    if(code == LV_EVENT_CLICKED)
        // LV_LOG_USER("Clicked");
        printf("anjian4\n");
}

// 作业1:4个按键的demo
void lv_zuoye1_btn_1(void)
{
    lv_obj_t * label;

    /*btn1的按键*/
    // 创建一个按键的对象
    lv_obj_t * btn1 = lv_btn_create(lv_scr_act());
    // 添加一个按键的时间
    // lv_obj_add_event_cb(btn1, event_handler1, LV_EVENT_ALL, NULL);
    lv_obj_add_event_cb(btn1, event_handler1, LV_EVENT_ALL, (void *)"10.1happy");
    // 添加按键的位置(可以自定义按照代码里面的宏LV_ALIGN_CENTER)
    lv_obj_align(btn1, LV_ALIGN_CENTER, -80, -80); // 左上

    // 创建一个label标签的对象(这个label是依附在btn1上面的label)
    label = lv_label_create(btn1);
    // 设置label的文本信息(暂时先用英文)
    lv_label_set_text(label, "anjian1");
    // 设置文本的位置
    lv_obj_center(label);

    /*btn2的按键*/
    lv_obj_t * btn2 = lv_btn_create(lv_scr_act());
    lv_obj_add_event_cb(btn2, event_handler2, LV_EVENT_ALL, NULL);
    lv_obj_align(btn2, LV_ALIGN_CENTER, -80, 80); // 左下
    // lv_obj_add_flag(btn2, LV_OBJ_FLAG_CHECKABLE);
    // lv_obj_set_height(btn2, LV_SIZE_CONTENT);

    label = lv_label_create(btn2);
    lv_label_set_text(label, "anjian2");
    lv_obj_center(label);

    /*btn3的按键*/
    lv_obj_t * btn3 = lv_btn_create(lv_scr_act());
    lv_obj_add_event_cb(btn3, event_handler3, LV_EVENT_ALL, NULL);
    lv_obj_align(btn3, LV_ALIGN_CENTER, 80, -80); // 右上

    label = lv_label_create(btn3);
    lv_label_set_text(label, "anjian3");
    lv_obj_center(label);

    /*btn4的按键*/
    lv_obj_t * btn4 = lv_btn_create(lv_scr_act());
    lv_obj_add_event_cb(btn4, event_handler4, LV_EVENT_ALL, NULL);
    lv_obj_align(btn4, LV_ALIGN_CENTER, 80, 80); // 右下

    label = lv_label_create(btn4);
    lv_label_set_text(label, "anjian3");
    lv_obj_center(label);
}

// 图标按钮在(x,y)的坐标位置 放了一个w宽度*h高度大小的按键
lv_obj_t * lv_bling_imgbtn(int x, int y, int w, int h, char * pic_path)
{
    /*Darken the button when pressed and make it wider*/
    // 添加按键的样式表，使他有一个动态的效果
    static lv_style_t style_pr; // 样式表
    lv_style_init(&style_pr);
    lv_style_set_img_recolor_opa(&style_pr, LV_OPA_30);
    lv_style_set_img_recolor(&style_pr, lv_color_black());
    lv_style_set_transform_height(&style_pr, 20);

    /*Create an image button*/
    lv_obj_t * imgbtn = lv_imgbtn_create(lv_scr_act());
    if(imgbtn == NULL) {
        LV_LOG_ERROR("lv_imgbtn_create fail:"); // 输出错误提示
        return NULL;
    }

    // 设置按键的大小
    lv_obj_set_size(imgbtn, w, h);
    lv_imgbtn_set_src(imgbtn, LV_IMGBTN_STATE_RELEASED, NULL, pic_path, NULL);
    // lv_obj_add_style(imgbtn1, &style_def, 0);
    lv_obj_add_style(imgbtn, &style_pr, LV_STATE_PRESSED);

    //(x和h按键偏移的位置)
    lv_obj_align(imgbtn, LV_ALIGN_TOP_LEFT, x, y);

    return imgbtn;
}
// 按键的回调函数
static void video_btn_event_callback(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);

    // 点击按键之后所处理的事情在这个if语句里面实现
    if(code == LV_EVENT_CLICKED) {
        printf("video.png\n");
        // LV_LOG_USER("Clicked");
    }
}

// 显示图像按钮
void test_btn_image()
{
    // 3.在主界面中创建音乐与视频图标(注意：要使用开发板里面的绝对路径 图片的大小是200*200)
    lv_obj_t * video = lv_bling_imgbtn(100, 100, 200, 200, "S:/IOT/bling/200.png");
    lv_obj_add_event_cb(video, video_btn_event_callback, LV_EVENT_CLICKED, NULL);
}

/*********************************label***********************************/
void lv_bling_label_1(void)
{
    lv_obj_t * label = lv_label_create(lv_scr_act());
    // 设置文本内容
    lv_label_set_text(label, "Hello, LVGL!");

    // 使能字体的颜色
    // lv_label_set_recolor(label, true);
    // lv_label_set_text(label,"#0000ff led#"); //设置文本颜色的格式

    // 设置label的大小：
    lv_obj_set_width(label, 300); // 设置宽度为300px
    lv_obj_set_height(label, 50); // 设置高度为50px

    // 设置字体的大小
    lv_obj_set_style_text_font(label, &lv_font_montserrat_30, 0);

    // 设置标签的位置
    lv_obj_set_pos(label, 100, 100);
}

// 滚动字体的效果
void lv_bling_label_2(void)
{
    lv_obj_t * label = lv_label_create(lv_scr_act());
    // 设置标签的模式为滚动的模式
    lv_label_set_long_mode(label, LV_LABEL_LONG_SCROLL_CIRCULAR); /*Circular scroll*/
    // 设置label显示的宽度大小
    lv_obj_set_width(label, 800);
    lv_label_set_recolor(label, true);
    // 注意：字体显示的内容要超过你自定义的区域大小
    lv_label_set_text(
        label,
        "#0000ff It is a circularly scrolling text.dsakjdhaskjdhkjashdkahdakjshdkasjdhjkshdkj#"); // 设置文本颜色的格式
    lv_obj_set_style_text_font(label, &lv_font_montserrat_40, 0);
    // lv_label_set_text(label, "It is a circularly scrolling text. ");
    lv_obj_align(label, LV_ALIGN_TOP_LEFT, 0, 240);
}

/*********************************img***********************************/
void lv_bling_image_1(void)
{
    /*
        如何实现jpg图片
        显示jpg在坐标(0,0)的位置放了一张200_200.jpg图
    */
    // 实例化一个img的对象
    lv_obj_t * img1 = lv_img_create(lv_scr_act());
    // 将图片放在lcd(0,0)的坐标位置
    lv_obj_set_pos(img1, 0, 0);
    // 设置图片的路劲(注意图片的路径要和开发板里面的路径一致)
    lv_img_set_src(img1, "S:/IOT/bling/jige.jpg");
    printf("show jpg200\n");
}

/*********************************gif***********************************/
void lv_bling_gif_1(void)
{
    /*
        如何实现gif动态图的控件
    */
    // 显示gif在坐标(200,0)的位置放了一张gif图
    // 实例化一个gif的对象
    lv_obj_t * gif1 = lv_gif_create(lv_scr_act());
    // 设置gif图的坐标位置
    lv_obj_set_pos(gif1, 0, 0);
    // lv_obj_align(gif1, LV_ALIGN_RIGHT_MID, -20, 0); //在起始坐标的位置左移20
    // 设置gif图的路径
    lv_gif_set_src(gif1, "S:/IOT/bling/kawa.gif");
    printf("show gif\n");
}

/*********************************msgbox***********************************/
static void event_cb(lv_event_t * e)
{
    lv_obj_t * obj = lv_event_get_current_target(e);
    LV_LOG_USER("Button %s clicked", lv_msgbox_get_active_btn_text(obj));

    // 如果是lv_msgbox_get_active_btn_text(obj) == "apply" 表示确定
    // 如果是lv_msgbox_get_active_btn_text(obj) == "close" 表示不确定
    // 用strcmp函数
    const char * ret_str = lv_msgbox_get_active_btn_text(obj);
    if(strcmp(ret_str, "Apply") == 0) printf("按确定按键之后需要处理的事情\n");
    if(strcmp(ret_str, "Close") == 0) printf("按取消按键之后需要处理的事情\n");
}

void lv_bling_msgbox_1(void)
{
    static const char * btns[] = {"Apply", "Close", ""};
    /*
        "Hello"是title标签
        "This is a message box with two buttons."是文本信息
    */
    lv_obj_t * mbox1 = lv_msgbox_create(NULL, "Hello", "This is a message box with two buttons.", btns, true);
    lv_obj_add_event_cb(mbox1, event_cb, LV_EVENT_VALUE_CHANGED, NULL);
    lv_obj_center(mbox1);
}

/*********************************list列表***********************************/
static lv_obj_t * list1; // 回调函数和主函数都要使用所以定义成全局变量

static void event_list_handler(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t * obj       = lv_event_get_target(e);
    if(code == LV_EVENT_CLICKED) {
        LV_LOG_USER("Clicked: %s", lv_list_get_btn_text(list1, obj));
    }
}

void lv_bling_list_1(void)
{
    /*Create a list*/
    list1 = lv_list_create(lv_scr_act());
    lv_obj_set_size(list1, 180, 220);
    lv_obj_center(list1);

    /*Add buttons to the list*/
    lv_obj_t * btn;

    lv_list_add_text(list1, "File");
    btn = lv_list_add_btn(list1, LV_SYMBOL_FILE, "New");
    lv_obj_add_event_cb(btn, event_list_handler, LV_EVENT_CLICKED, NULL);
    btn = lv_list_add_btn(list1, LV_SYMBOL_DIRECTORY, "Open");
    lv_obj_add_event_cb(btn, event_list_handler, LV_EVENT_CLICKED, NULL);
    btn = lv_list_add_btn(list1, LV_SYMBOL_SAVE, "Save");
    lv_obj_add_event_cb(btn, event_list_handler, LV_EVENT_CLICKED, NULL);
    btn = lv_list_add_btn(list1, LV_SYMBOL_CLOSE, "Delete");
    lv_obj_add_event_cb(btn, event_list_handler, LV_EVENT_CLICKED, NULL);
    btn = lv_list_add_btn(list1, LV_SYMBOL_EDIT, "Edit");
    lv_obj_add_event_cb(btn, event_list_handler, LV_EVENT_CLICKED, NULL);

    lv_list_add_text(list1, "Connectivity");
    btn = lv_list_add_btn(list1, LV_SYMBOL_BLUETOOTH, "Bluetooth");
    lv_obj_add_event_cb(btn, event_list_handler, LV_EVENT_CLICKED, NULL);
    btn = lv_list_add_btn(list1, LV_SYMBOL_GPS, "Navigation");
    lv_obj_add_event_cb(btn, event_list_handler, LV_EVENT_CLICKED, NULL);
    btn = lv_list_add_btn(list1, LV_SYMBOL_USB, "USB");
    lv_obj_add_event_cb(btn, event_list_handler, LV_EVENT_CLICKED, NULL);
    btn = lv_list_add_btn(list1, LV_SYMBOL_BATTERY_FULL, "Battery");
    lv_obj_add_event_cb(btn, event_list_handler, LV_EVENT_CLICKED, NULL);

    lv_list_add_text(list1, "Exit");
    btn = lv_list_add_btn(list1, LV_SYMBOL_OK, "Apply");
    lv_obj_add_event_cb(btn, event_list_handler, LV_EVENT_CLICKED, NULL);
    btn = lv_list_add_btn(list1, LV_SYMBOL_CLOSE, "Close");
    lv_obj_add_event_cb(btn, event_list_handler, LV_EVENT_CLICKED, NULL);
}

/*********************************keyboard键盘***********************************/
static void ta_event_cb(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t * ta        = lv_event_get_target(e);
    lv_obj_t * kb        = lv_event_get_user_data(e);
    if(code == LV_EVENT_FOCUSED) {
        lv_keyboard_set_textarea(kb, ta);
        lv_obj_clear_flag(kb, LV_OBJ_FLAG_HIDDEN);
    }

    if(code == LV_EVENT_DEFOCUSED) {
        lv_keyboard_set_textarea(kb, NULL);
        lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN);
    }
    // const char * text = lv_textarea_get_text(ta); // 获取当前输入的文本
    // LV_LOG_USER("输入内容: %s", text);
}

void lv_bling_keyboard_1(void)
{
    /*Create a keyboard to use it with an of the text areas*/
    lv_obj_t * kb = lv_keyboard_create(lv_scr_act());

    /*Create a text area. The keyboard will write here*/
    lv_obj_t * ta;
    ta = lv_textarea_create(lv_scr_act());
    lv_obj_align(ta, LV_ALIGN_TOP_LEFT, 10, 10);
    lv_obj_add_event_cb(ta, ta_event_cb, LV_EVENT_ALL, kb);
    lv_textarea_set_placeholder_text(ta, "Hello");
    lv_obj_set_size(ta, 140, 80); // 140*80

    ta = lv_textarea_create(lv_scr_act());
    lv_obj_align(ta, LV_ALIGN_TOP_RIGHT, -10, 10);
    lv_obj_add_event_cb(ta, ta_event_cb, LV_EVENT_ALL, kb);
    lv_obj_set_size(ta, 140, 80);

    lv_keyboard_set_textarea(kb, ta);
}

/*********************************text文本框***********************************/
static void textarea_event_handler(lv_event_t * e)
{
    lv_obj_t * ta = lv_event_get_target(e);
    LV_LOG_USER("Enter was pressed. The current text is: %s", lv_textarea_get_text(ta));
}

static void btnm_event_handler(lv_event_t * e)
{
    lv_obj_t * obj   = lv_event_get_target(e);
    lv_obj_t * ta    = lv_event_get_user_data(e);
    const char * txt = lv_btnmatrix_get_btn_text(obj, lv_btnmatrix_get_selected_btn(obj));

    if(strcmp(txt, LV_SYMBOL_BACKSPACE) == 0)
        lv_textarea_del_char(ta);
    else if(strcmp(txt, LV_SYMBOL_NEW_LINE) == 0)
        lv_event_send(ta, LV_EVENT_READY, NULL);
    else
        lv_textarea_add_text(ta, txt);
}

void lv_bling_textarea_1(void)
{
    lv_obj_t * ta = lv_textarea_create(lv_scr_act());
    lv_textarea_set_one_line(ta, true);
    lv_obj_align(ta, LV_ALIGN_TOP_MID, 0, 10);
    lv_obj_add_event_cb(ta, textarea_event_handler, LV_EVENT_READY, ta);
    lv_obj_add_state(ta, LV_STATE_FOCUSED); /*To be sure the cursor is visible*/

    static const char * btnm_map[] = {
        "1", "2", "3", "\n", "4", "5", "6", "\n", "7", "8", "9", "\n", LV_SYMBOL_BACKSPACE, "0", LV_SYMBOL_NEW_LINE,
        ""};

    lv_obj_t * btnm = lv_btnmatrix_create(lv_scr_act());
    lv_obj_set_size(btnm, 200, 150); // 设置键盘的大小
    lv_obj_align(btnm, LV_ALIGN_BOTTOM_MID, 0, -10);
    lv_obj_add_event_cb(btnm, btnm_event_handler, LV_EVENT_VALUE_CHANGED, ta);
    lv_obj_clear_flag(btnm, LV_OBJ_FLAG_CLICK_FOCUSABLE); /*To keep the text area focused on button clicks*/
    lv_btnmatrix_set_map(btnm, btnm_map);
}
#endif

int main(void)
{
    /*lvgl初始化*/
    lv_init();

    /*输出设备初始化及注册*/
    fbdev_init();
    /*A small buffer for LittlevGL to draw the screen's content*/
    static lv_color_t buf[DISP_BUF_SIZE];
    /*Initialize a descriptor for the buffer*/
    static lv_disp_draw_buf_t disp_buf;
    lv_disp_draw_buf_init(&disp_buf, buf, NULL, DISP_BUF_SIZE);
    /*Initialize and register a display driver*/
    static lv_disp_drv_t disp_drv;
    lv_disp_drv_init(&disp_drv);
    disp_drv.draw_buf = &disp_buf;
    disp_drv.flush_cb = fbdev_flush;
    disp_drv.hor_res  = 800;
    disp_drv.ver_res  = 480;
    lv_disp_drv_register(&disp_drv);

    // 输入设备初始化及注册
    evdev_init();
    static lv_indev_drv_t indev_drv_1;
    lv_indev_drv_init(&indev_drv_1); /*Basic initialization*/
    indev_drv_1.type = LV_INDEV_TYPE_POINTER;
    /*This function will be called periodically (by the library) to get the mouse position and state*/
    indev_drv_1.read_cb = evdev_read;
    lv_indev_drv_register(&indev_drv_1);
    // lv_indev_t * mouse_indev = lv_indev_drv_register(&indev_drv_1);

    // 官方demo---可以换为自己的demo
    //  lv_demo_widgets();

    // lv_bling_btn_1();
    // lv_zuoye1_btn_1(); // 作业1:4个按键
    // test_btn_image();
    // lv_bling_label_1();
    // lv_bling_label_2();
    // lv_bling_image_1();
    // lv_bling_gif_1();
    // lv_bling_msgbox_1();
    // lv_bling_list_1();
    // lv_bling_keyboard_1();
    // lv_bling_textarea_1();
    // lvgl_hezi();
    // lvgl_biankuang();
    // lvgl_backgroud();
    // lvgl_shadow();
    //  font_test();
    /*事物处理及告知lvgl节拍数*/
    //lv_zhuce();
    //  go_to_main_page();
    // sleep(1);
    //btn_wind();
    // show_main_win();
    ui_load_page(lv_zhuce);
    while(1) {
        lv_timer_handler(); // 事务处理
        lv_tick_inc(5);     // 节拍累计
        usleep(5000);
    }

    return 0;
}
